package edu.mama.college.service;

import edu.mama.college.mapper.CourseMapper;
import edu.mama.college.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 获取热门课程
     * @param limit
     * @return
     */
    public List<Course> getHotCourses(int limit){
        return courseMapper.getHotCourses(limit);
    }

    /**
     * 获取课程详情
     * @param courseId
     * @return
     */
    @Cacheable("CourseDetail")
    public Course getCourseDetail(int courseId){
        return courseMapper.getCourseDetail(courseId);
    }

    /**
     * 获取课程列表
     * @param page
     * @param size
     * @param name
     * @param teacher
     * @return
     */
    public Page<Course> courseList(int page,int size, String name,String teacher){
        List<Course> courseList = courseMapper.courseList(page,size,name,teacher);
        int total = courseMapper.courseTotal(page,size,name,teacher);
        return new Page<>(page,size,total,courseList);
    }


    /**
     * 新增或修改课程
     * @param course
     * @param id
     * @throws Exception
     */
    // 清除缓存
//    @CacheEvict(value = "CourseDetail",key = "#id")
    // 直接用方法的返回值更新缓存
    @CachePut(value= "CourseDetail",key="#course.id")
    public Course save(Course course,int id) throws Exception {


        if(course.getName() == null || course.getName().equals("")) {
            throw new Exception("课程名不能为空");
        }
        if(course.getPoster() == null || course.getPoster().equals("")){
            throw new Exception("课程封面不能为空");
        }
        if(course.getTeacher() == null || course.getTeacher().equals("")){
            throw new Exception("老师不能为空");
        }
        try{
            if(id > 0){
                courseMapper.update(course);
            }else{
                courseMapper.create(course);
            }
            // 为了实现直接更新缓存，这里将void的返回值改成Course
            return course;
        }catch (Exception e){
            log.error("发布课程失败:",e);
            throw new Exception("操作失败");
        }

    }

    @CacheEvict(value="CourseDetail",key="#id")
    public void delete(int id){
        courseMapper.delete(id);
    }

    @Transactional()
    @CacheEvict(value = "CourseDetail",key="#courseId")
    public void addLessons(List<Lesson> lessonList,int courseId){
        courseMapper.addLessons(lessonList);
    }

    @CacheEvict(value = "CourseDetail",key="#courseId")
    public void updateLesson(Lesson lesson,int courseId){
        courseMapper.updateLesson(lesson);
    }

    @CacheEvict(value = "CourseDetail",key="#courseId")
    public void deleteLesson(int lessonId,int courseId){
        courseMapper.deleteLesson(lessonId);
    }

    public Page<UserCourse> getCourseStudents(int page, int size, int courseId){
        int total = courseMapper.studentListByCourseCount(courseId);
        List<UserCourse> userList = courseMapper.studentListByCourse(page, size, courseId);

        return new Page<>(page, size, total, userList);
    }

    public void updateStudents(int courseId){
        courseMapper.updateCourseStudents(courseId);
    }

}


